import {
  delUserById,
  newUserData,
  postUserListData,
  upDateUserData
} from '@/service/main/system/system'
import { defineStore } from 'pinia'
import type { ISystemState } from './type'

const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersList: [],
    userTotalCount: 0
  }),
  actions: {
    async postUserListAction(queryInfo: any) {
      // 查询用户数据
      const usersListRes = await postUserListData(queryInfo)
      const { totalCount, list } = usersListRes.data
      this.userTotalCount = totalCount
      this.usersList = list
    },
    async delUserByIdAction(id: number) {
      // 1.删除用户数据操作
      const delRes = await delUserById(id)
      // 2.重新请求数据
      this.postUserListAction({ offset: 0, size: 10 })
    },
    async newUserDataAction(userinfo: any) {
      // 新建用户数据
      const res = await newUserData(userinfo)
      // 2.重新请求数据
      this.postUserListAction({ offset: 0, size: 10 })
    },
    async editUserDataAction(id: number, userInfo: any) {
      // 1.编辑用户数据操作
      const delRes = await upDateUserData(id, userInfo)
      // 2.重新请求数据
      this.postUserListAction({ offset: 0, size: 10 })
    }
  }
})

export default useSystemStore
